package com.carefree.bookmall.controller;

import com.carefree.bookmall.common.vo.BookQueryVo;
import com.carefree.bookmall.common.vo.PageVO;
import com.carefree.bookmall.entity.BookType;
import com.carefree.bookmall.exception.CustomException;
import com.carefree.bookmall.service.BookService;
import com.carefree.bookmall.service.BookTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName:BookController
 * @Description:
 * @Author Tengbin
 */
@Controller
@RequestMapping("/bookmall")
public class BookController {

    @Resource
    public BookService bookService;

    @Resource
    public BookTypeService bookTypeService;


    /**
     * 书籍详情页
     *
     * @return
     */
    @GetMapping("/detail/{bookId}")
    public String bookDetail(@PathVariable Integer bookId, HttpServletRequest request) throws CustomException {
        // 根据书籍id查询书籍信息
        BookQueryVo bookQueryVo = bookService.queryBookByBookId(bookId);

        request.setAttribute("book", bookQueryVo);

        return "book_detail";
    }


    @GetMapping("/quickview/{bookId}")
    @ResponseBody
    public BookQueryVo quickViewBookInfoById(@PathVariable Integer bookId, HttpServletRequest request) throws CustomException {
        // 根据书籍id查询书籍信息
        return bookService.queryBookByBookId(bookId);
    }


    /**
     * 商品分类
     *
     * @param pageNumber 页码
     * @param typeId     类别id
     * @param request    请求对象
     * @return
     */
    @GetMapping("/types/{typeUrl}/{pageNumber}")
    public String getBookTypes(@PathVariable String typeUrl, @PathVariable int pageNumber, HttpServletRequest request) throws CustomException {

        String typeName = "";

        PageVO pageVO = null;
        if ("all".equals(typeUrl)) {
            pageVO = bookTypeService.queryAllBooksForTypes(pageNumber, 12);
            if (pageVO.getTotalPage() == 0) {
                pageVO.setPageNumber(1);
                pageVO.setTotalPage(1);
            } else {
                if (pageNumber >= pageVO.getTotalPage() + 1) {
                    pageVO = bookTypeService.queryAllBooksForTypes(pageVO.getTotalPage(), 12);
                }
            }
        } else {
            // 根据类别的访问路径查询类别信息
            BookType bookType = bookTypeService.queryBookTypeUrl(typeUrl);

            typeName = bookType.getTypeName();

            // 根据类别id分页查询
            pageVO = bookTypeService.queryBookByBooktypeId(bookType.getId(), pageNumber, 8);
            if (pageVO.getTotalPage() == 0) {
                pageVO.setPageNumber(1);
                pageVO.setTotalPage(1);
            } else {
                if (pageNumber >= pageVO.getTotalPage() + 1) {
                    pageVO = bookTypeService.queryBookByBooktypeId(bookType.getId(), pageVO.getTotalPage(), 8);
                }
            }
            request.setAttribute("typeId", bookType.getId());
        }

        request.setAttribute("bookTypes", bookTypeService.queryBookTypes());
        request.setAttribute("typeName", typeName);
        request.setAttribute("page", pageVO);

        return "booktypes";
    }


    /**
     * 热销/新品
     *
     * @param recommendType 推荐类型
     * @param pageNumber    页数
     * @param request
     * @return
     */
    @GetMapping("/recommend/{recommendType}/{pageNumber}")
    public String showRecommendBooks(@PathVariable String recommendType, @PathVariable Integer pageNumber, HttpServletRequest request) throws CustomException {

        PageVO pageVO = bookService.pageBookByRecommendType(recommendType, pageNumber, 8);

        if (pageVO.getTotalPage() == 0) {
            pageVO.setTotalPage(1);
            pageVO.setPageNumber(1);
        } else {
            if (pageNumber >= pageVO.getTotalPage() + 1) {
                pageVO = bookService.pageBookByRecommendType(recommendType, pageVO.getTotalPage(), 8);
            }
        }
        request.setAttribute("page", pageVO);
        request.setAttribute("recommendType", recommendType);
        request.setAttribute("bookTypes", bookTypeService.queryBookTypes());

        return "recommends";
    }


}
